<?php
/**
 * @title 909. 蛇梯棋
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $board
     * @return Integer
     */
    function snakesAndLadders($board) {
        /**
         * @since 2023-12-28 矩阵边长
         */
        $n = count($board);

        /**
         * @since 2023-12-28 方格位置对应传送门
         */
        $positionMap = [];

        /**
         * @since 2023-12-28 向右的方向
         */
        $right = true;

        /**
         * @since 2023-12-28 迭代，生成方格位置对应传送门
         */
        $curr = 1;
        for($r=$n-1; $r>=0; $r--){
            /**
             * @since 2023-12-28 从左到右
             */
            if($right){
                for($c=0; $c<$n; $c++){
                    $positionMap[$curr] = $board[$r][$c];
                    $curr++;
                }
            } else {
                /**
                 * @since 2023-12-28 从右到左
                 */
                for($c=$n-1; $c>=0; $c--) {
                    $positionMap[$curr] = $board[$r][$c];
                    $curr++;
                }
            }

            /**
             * @since 2023-12-28 改变方向
             */
            $right = !$right;
        }

        /**
         * @since 2023-12-28 前进最大值
         */
        $max = $n * $n;

        /**
         * @since 2023-12-28 生成一个数组，记录每个位置的最小步数
         */
        $minStepArray = array_fill(1, $max, PHP_INT_MAX);
        $minStepArray[1] = 0;

        /**
         * @since 2023-12-28 从起点1出发
         */
        $pointArray = [1];
        while($pointArray){
            /**
             * @since 2023-12-28 下次出发的点集合
             */
            $nextPointArray = [];

            /**
             * @since 2023-12-28 遍历当前出发的点
             */
            foreach ($pointArray as $point){
                // echo "\n\n\npoint: {$point}\n";
                /**
                 * @since 2023-12-28 到达nextPoint的次数 = 到达point的次数 + 1
                 */
                $nextStep = $minStepArray[$point] + 1;

                /**
                 * @since 2023-12-28 每次前进1-6步
                 */
                for($nextPoint=$point+1; $nextPoint<=min($point+6, $max); $nextPoint++){
                    /**
                     * @since 2023-12-28 检查是否有传送门
                     */
                    $boardNum = $positionMap[$nextPoint];
                    // echo "nextPoint: {$nextPoint}, boardNum: {$boardNum}\n";
                    if($boardNum > 0){
                        $endPoint = $boardNum;
                    } else {
                        $endPoint = $nextPoint;
                    }

                    /**
                     * @since 2023-12-28 更少的次数，有继续前进的意义
                     */
                    if($nextStep < $minStepArray[$endPoint]){
                        $minStepArray[$endPoint] = $nextStep;
                        $nextPointArray[] = $endPoint;
                    } else {}
                }
            }

            /**
             * @since 2023-12-28 下次出发的点
             */
            $pointArray = $nextPointArray;
        }
        // var_dump($minStepArray);

        /**
         * @since 2023-12-28 无法到达终点
         * @return Integer
         */
        if($minStepArray[$max] === PHP_INT_MAX){
            return -1;
        } else {
            return $minStepArray[$max];
        }
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.28_5.php
 */
$datas = [
    [[[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]]], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->snakesAndLadders(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
